﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator.ConsoleCalc
{
    class Validator: IValidator
    {
        Boolean _isValid;
        String _validString;
        String _originalString;
    	private Boolean _isNumber;
		protected Char[] ValidSymbols;

        public Validator()
        {
            ValidSymbols = new Char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '(', ')', ',' };
        }

        public void Validate(string argument)
        {
            _originalString = argument;
            _validString = "";
            _isValid = true;

            argument = TrimSpace(argument);
            argument = TrimDoubleOperators(argument);

			if (isEmpty(argument))
				_isValid = false;
			else if (!isValidSymbols(argument))
				_isValid = false;
			else if (!isValidBrackets(argument))
				_isValid = false;

			if (_isValid)
			{
				_isNumber = isNumber_m(argument);
				_validString = argument;
			}
        }

		private bool isNumber_m(string args)
		{
			var isnumber = false;
			if (args[0] == '-' || args[0]=='+'|| Char.IsDigit(args[0]))
			{
				isnumber = true;
				for (int i = 1; i< args.Length; i++)
				{
					if (!Char.IsDigit(args[i]))
						isnumber = false;
				}
			}
			return isnumber;
		}

        public bool isValid
        {
            get { return _isValid; }
        }

        public string ValidString
        {
            get { return _validString; }
        }

        public string OriginalString
        {
            get { return _originalString; }
        }

    	public bool isNumber
    	{
    		get { return _isNumber; }
    	}


    	private bool isEmpty(string args)
        {
            if (args == "")
            {
                return true;
            }
            return false;
        }
        private bool isValidBrackets(string args)
        {
            int open = 0;

            foreach (Char ch in args)
            {
                if (ch == '(')
                    open++;
                if (ch == ')')
                    open--;
            }

            return open == 0;
        }
        private bool isValidSymbols(string args)
        {
            foreach (Char ch in args)
            {
                if (!ValidSymbols.Contains(ch))
                    return false;
            }
            return true;
        }
        private string TrimDoubleOperators(string args)
        {
            int prevLength = 0;
            do
            {
                prevLength = args.Length;
                args = args.Replace("--", "+");
                args = args.Replace("++", "+");
                args = args.Replace("-+", "-");
                args = args.Replace("+-", "-");
            } while (prevLength != args.Length);
            return args;
        }
        private string TrimSpace(string args)
        {
            int prevLength = 0;
            do
            {
                prevLength = args.Length;
                args = args.Replace(" ", "");
            } while (prevLength != args.Length);
            return args;
        }
    }
}
